package com.hh.bss.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;


public class PackageUtil {

	private static int tt=0;
	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		
		//Reflect_Object("com.tb.bss.auth.controller.SystemUserController");
		//if (true) return ;
		List<String> cls = getClassInPackage("com.tb.bss");
		for (String s : cls) {
			//System.out.println(s);
			Reflect_Object(s);
		}

	}
    public static void loopFile(String classdir,File dir,List<String> ret ){
    	
    	for (File file : dir.listFiles()) {
			if (file.isFile()) {
				String clsName = file.getAbsolutePath();
				String cName=clsName.substring(classdir.length()+1);
				cName=cName.substring(0, cName.length()-6);
				cName=cName.replaceAll("\\\\", ".");
				//System.out.println(classdir+":class:"+cName);
//				clsName = pkgName
//						+ "."
//						+ clsName
//								.substring(0, clsName.length() - 6);
				if (cName.indexOf(".controller.")>0)
				ret.add(cName);
			}else{
				loopFile(classdir,file,ret);
			}
		}
    }
    public static void Reflect_Object(String classPath){
        try {
            Class userClass = Class.forName(classPath);//加载类
               Method[] methods = userClass.getDeclaredMethods();//获得类的方法集合       
               //遍历方法集合
               for(int i =0 ;i<methods.length;i++){
                  //获取所有getXX()的返回值
                  //methods[i].getName()方法返回方法名
            	   String methodName=methods[i].getName();
            	   if (methodName.startsWith("ext")){
            		 //  String url=classPath.r
            	   }
            	   System.out.println(classPath+" "+methods[i].getName());
                  if(methods[i].getName().startsWith("get")){
                      //Object object = methods[i].invoke(o, null);
                     // System.out.println(" "+methods[i].getName()+"="+object);
                  }
               }
               System.out.println("====");
           } catch (Exception e) {
               e.printStackTrace();
           }
    }
	public static List<String> getClassInPackage(String pkgName) {
		List<String> ret = new ArrayList<String>(10000);
		//String rPath = pkgName.replace('.', '/') + "/";
		String rPath = "\\"+pkgName.replace('.', '\\')+"\\" ;
		try {
			for (File classPath : CLASS_PATH_ARRAY) {
				//System.out.println(classPath.getAbsolutePath());
				if (classPath.getAbsolutePath().indexOf("WEB-INF\\classes")>=0) {
					System.out.println("WEB-INF\\classes");
				}
				if (!classPath.exists())
					continue;
				if (classPath.isDirectory()) {
					File dir = new File(classPath, rPath);
					if (!dir.exists())
						continue;
					
					loopFile(classPath.getAbsolutePath(),dir,ret);
//					for (File file : dir.listFiles()) {
//						if (file.isFile()) {
//							String clsName = file.getName();
//							clsName = pkgName
//									+ "."
//									+ clsName
//											.substring(0, clsName.length() - 6);
//							ret.add(clsName);
//						}
//					}
				} else {
					FileInputStream fis = new FileInputStream(classPath);
					JarInputStream jis = new JarInputStream(fis, false);
					JarEntry e = null;
					while ((e = jis.getNextJarEntry()) != null) {
						String eName = e.getName();
						if (eName.startsWith(rPath) && !eName.endsWith("/")) {
							ret.add(eName.replace('/', '.').substring(0,
									eName.length() - 6));
						}
						jis.closeEntry();
					}
					jis.close();
				}
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}

		return ret;
	}

	private static String[] CLASS_PATH_PROP = { "java.class.path",
			"java.ext.dirs", "sun.boot.class.path" };

	private static List<File> CLASS_PATH_ARRAY = getClassPath();

	private static List<File> getClassPath() {
		List<File> ret = new ArrayList<File>();
		String delim = ":";
		if (System.getProperty("os.name").indexOf("Windows") != -1)
			delim = ";";
		for (String pro : CLASS_PATH_PROP) {
			String[] pathes = System.getProperty(pro).split(delim);
			for (String path : pathes){
				//System.out.println(path);
				ret.add(new File(path));
			}	
		}
		return ret;
	}
}
